const Base=require('./base');
const assert = require('assert');

module.exports = class extends Base
{
    static get _REST()
    {
        return true;
    }

    constructor(ctx)
    {
        super(ctx);
        this.resource = this.getResource();
        this._guid = this.getGuid();
        assert(think.isFunction(this.model), 'this.model must be a function');
        this.modelInstance = this.model(this.resource);
    }

    __before()
    {
    }

    /**
     * get resource
     * @return {String} [resource name]
     */
    getResource()
    {
        return this.ctx.controller;
    }

    getGuid()
    {
        const _guid = this.get('guid');
        if (_guid && think.isString(_guid))
        {
            return _guid;
        } else
        {
            return '';
        }
        // const last = this.ctx.path.split('/').slice(-1)[0];
        // if (last !== this.resource) {
        //   return last;
        // }

    }

    async getAction()
    {

        // let data;
        // if (this._guid)
        // {
        //     const pk = this.modelInstance.pk;
        //     data = await this.modelInstance.where({[pk]: this._guid}).find();
        //     return this.success(data);
        // }
        // data = await this.modelInstance.select();
        // return this.success(data);
    }

    /**
     * put resource
     * @return {Promise} []
     */
    async postAction()
    {
        // const pk = this.modelInstance.pk;
        // const data = this.post();
        // delete data[pk];
        // if (think.isEmpty(data))
        // {
        //     return this.fail('data is empty');
        // }
        // const insertId = await this.modelInstance.add(data);
        // return this.success({_guid: insertId});
    }

    /**
     * delete resource
     * @return {Promise} []
     */
    async deleteAction()
    {
        // if (!this._guid)
        // {
        //     return this.fail('params error');
        // }
        // const pk = this.modelInstance.pk;
        // const rows = await this.modelInstance.where({[pk]: this._guid}).delete();
        // return this.success({affectedRows: rows});
    }

    /**
     * update resource
     * @return {Promise} []
     */
    async putAction()
    {
        // if (!this._guid)
        // {
        //     return this.fail('params error');
        // }
        // const pk = this.modelInstance.pk;
        // const data = this.post();
        // delete data[pk];
        // if (think.isEmpty(data))
        // {
        //     return this.fail('data is empty');
        // }
        // const rows = await this.modelInstance.where({[pk]: this._guid}).update(data);
        // return this.success({affectedRows: rows});
    }

    __call()
    {
    }

    //总错误提示方法
    error(errNo,errMsg){
        this.fail(errNo,errMsg);
        return false;
    }


    //成功提示方法
    end(data){
        if(think.isEmpty(data)){
            this.ctx.fail(500,'暂无数据');
            return false;
        }
        let resData=data?data:0;
        return this.success(data);
    }
};